package per.srp.demo.carLink;

import per.srp.demo.carLink.entity.Car;
import per.srp.demo.carLink.entity.CarAndSocket;
import per.srp.demo.carLink.entity.CarCommand;

import java.util.*;

public class CarCommandHandler {

    private static Map<String, LinkedList<CarCommand>> carCommands = new HashMap<>();

    //插入数据
    public static void pushCarCommand(Car car,CarCommand command){
        LinkedList<CarCommand> carCommandList = carCommands.get(car.getNo());
        if(carCommandList==null){
            carCommandList = new LinkedList<CarCommand>();
            carCommands.put(car.getNo(),carCommandList);
        }
        carCommandAdd(carCommandList,command);
    }

    private static synchronized void carCommandAdd(LinkedList<CarCommand> carCommandList,CarCommand command){
        if(carCommandList!=null&&command!=null)
        carCommandList.add(command);
    }

    //插入数据
    synchronized static public void pushCarCommand(Car car,CarCommand[] commands){
        if(car.getNo()==null||commands==null||commands.length==0) return;;
        LinkedList<CarCommand> carCommandList = carCommands.get(car.getNo());
        if(carCommandList==null){
            carCommandList = new LinkedList<CarCommand>();
            carCommands.put(car.getNo(),carCommandList);
        }
        for(CarCommand eachCommand : commands){
            carCommandAdd(carCommandList,eachCommand);
        }
    }

    //读取小车命令
    public static LinkedList<CarCommand> popCarCommand(Car c){
        LinkedList<CarCommand> commandLinkedList = carCommands.get(c.getNo());
        if(commandLinkedList!=null){
            carCommands.put(c.getNo(),null);
        }
        return commandLinkedList;
    }

    //读取小车命令
    public static LinkedList<CarCommand> queryCarCommand(Car c){
        LinkedList<CarCommand> commandLinkedList = carCommands.get(c.getNo());
        return commandLinkedList;
    }
}
